import cn.gyx.hrm.SearchApplication;
import cn.gyx.hrm.doc.CourseDoc;
import cn.gyx.hrm.repository.CourseElasticSearchRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Optional;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SearchApplication.class)
public class EStest {

    // 注入es
    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @Autowired
    private CourseElasticSearchRepository repository;

    @Test
    public void testName(){
        // 创建索引库和映射
        elasticsearchTemplate.createIndex(CourseDoc.class);

        elasticsearchTemplate.putMapping(CourseDoc.class);

    }

    // 添加数据
    // 如果ID已经在ES中存在，添加就是修改
    // 或者删除再添加
    @Test
    public void add(){
        CourseDoc doc = new CourseDoc();
        doc.setId(1L);
        doc.setName("6666");
        doc.setGradeName("test");
        repository.save(doc);
    }
    // 获取
    @Test
    public void get(){
        Optional<CourseDoc> optional = repository.findById(1L);
        CourseDoc doc = optional.get();
        System.out.println(doc);
    }
    // 删除
    @Test
    public void delete(){
        repository.deleteById(1L);
        get();
    }


}